import CryptoJS from 'crypto-js'

const key = CryptoJS.enc.Utf8.parse('tecsuntecsun1234') // 十六位十六进制数作为密钥

const aes = {
  AES_Encrypt(word) {
    if (!word) {
      return word
    }
    var srcs = CryptoJS.enc.Utf8.parse(word)
    var encrypted = CryptoJS.AES.encrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
    var hexStr = encrypted.ciphertext.toString()
    var oldHexStr = CryptoJS.enc.Hex.parse(hexStr)
    // 将密文转为Base64的字符串
    var base64Str = CryptoJS.enc.Base64.stringify(oldHexStr)
    return base64Str
  },
  AES_Decrypt(word) {
    if (!word) {
      return word
    }
    var srcs = word
    var decrypt = CryptoJS.AES.decrypt(srcs, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 })
    var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8)
    var value = decryptedStr.toString()
    return value
  }
}

export default aes
